package student.mindmap.core.model;

import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.*;

class MindMapTest {
    private MindMap map;
    private MindMapNode root;

    @BeforeEach
    void setup() {
        map = new MindMap();
        map.createNewMap("中心主题");
        root = map.getRoot();
    }

    @Test
    void testCreateNewMap() {
        assertEquals("中心主题", root.getText());
        assertEquals(1, map.getAllNodes().size());
    }

    @Test
    void testAddNodes() {
        MindMapNode child1 = new MindMapNode("子节点1");
        root.addChild(child1);
        map.registerNode(child1);

        MindMapNode child2 = new MindMapNode("子节点2");
        child1.addChild(child2);
        map.registerNode(child2);

        assertEquals(3, map.getAllNodes().size());
        assertEquals(1, root.getChildren().size());
        assertEquals(1, child1.getChildren().size());
    }

    @Test
    void testDeleteNode() {
        MindMapNode child = new MindMapNode("待删除节点");
        root.addChild(child);
        map.registerNode(child);

        map.deleteNode(child);
        assertEquals(1, map.getAllNodes().size());
        assertTrue(root.getChildren().isEmpty());
    }

    @Test
    void testFindNode() {
        MindMapNode target = new MindMapNode("目标节点");
        root.addChild(target);
        map.registerNode(target);

        MindMapNode found = map.findNodeById(target.getId());
        assertNotNull(found);
        assertEquals("目标节点", found.getText());
    }

    @Test
    void testUpdateNode() {
        root.setText("修改后的中心主题");
        assertEquals("修改后的中心主题", map.getRoot().getText());
    }
}